<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/29
 * Time: 9:32
 */

namespace Home\Controller;

use Home\Common\Controller\LoginedController;
use Think\Exception;

/**
 * Class UserinfoController
 * @package Home\Controller
 * 用户信息管理
 */
class UserinfoController extends LoginedController
{
   /**
    * 地址列表
    */
    public  function addr(){
        //找出该用户的所有地址
        $uid=session('uid');
          $addrs=M('useraddr')->where("uid=$uid")->select();
        $this->assign('addrs',$addrs);
//        dd($addrs);
        $this->display();
    }
    /**
     * 地址添加,编辑
     */
    public function addaddr(){
        $udid=I('get.udid');
        if($udid){
            //编辑，查找旧数据
            $olddata=M('useraddr')->where("udid=$udid")->find();
            $olddata['useraddr']=json_decode($olddata['useraddr'],true);
            $this->assign('olddata',$olddata);
            dd($olddata);
        }
        if(IS_POST){
            if($udid){
               //表示修改
                $_POST['udid']=$udid;
            }
            $_POST['useraddr']=json_encode($_POST['useraddr'],JSON_UNESCAPED_UNICODE);
            $_POST['uid']=session('uid');
            $res=D('useraddr')->store($_POST);
            if($res['valid']){
                $this->ajaxReturn(['valid'=>1,'msg'=>$res['msg']]);die;
            }else{
                $this->ajaxReturn(['valid'=>0,'msg'=>$res['msg']]);die;
            }
//          dd($_POST);die;
        }
        $this->display();
    }

    /**
     *订单列表
     */
    public function order(){
//        查找该用户的订单信息
        $uid=session('uid');
       $orders=M('payorder')
           ->join("sp_userorder ON sp_payorder.uoid=sp_userorder.uoid")
           ->where("sp_payorder.uid=$uid")->select();
//        $goods=json_decode($orders[''])
         $goods=[];
        foreach($orders as $k=>$v){
           $goods[]=[
               'orderstatus'=>$v['orderstatus'],
               'zongjian'=>$v['count'],
               'uoid'=>$v['uoid'],
               'orderpay'=>$v['orderpay'],
               'ordernumber'=>$v['ordernumber'],
               'value'=>json_decode($v['orderinfo'],true),
           ];
        }
        //更新信息中的个status；
        foreach($goods as $k=>$v){
            foreach($v['value'] as $m=>$n){
                $goods[$k]['value'][$m]['gstatus']=M('usergoods')->where("ugid={$n['ugid']}")->getField("gstatus");
            }
        }
        $this->assign('goods',$goods);
//        dd($orders);
//        dd($goods);
        $this->display();
    }

    /**
     * 个人信息添加和修改
     */
    public function store(){
        if(IS_AJAX){
            //判断是否为手机号
            if(isPHone(I('post.phone'))){
              $this->ajaxReturn(['valid'=>0,'msg'=>'手机号格式不对']);die;
            }
//            dd(I('post.addres'));die;
            //判断地址是否为空
            foreach(I('post.addres') as $k=>$v){
                    if( empty($v)){
                        $this->ajaxReturn(['valid'=>0,'msg'=>'请选择地址']);die;
                    }
            }
            $_POST['uid']=session('uid');
            $_POST['addres']=json_encode($_POST['addres'],JSON_UNESCAPED_UNICODE);
//            $_POST['addres']=json_decode($_POST['addres'],true);
//            dd(I('post.'));die;
            $res=D('userinfo')->store($_POST);
//            dd($res);die;
          if($res['valid']){
             $this->ajaxReturn(['valid'=>1,'msg'=>$res['msg']]);die;
          }else{
              $this->ajaxReturn(['valid'=>0,'msg'=>$res['msg']]);die;
          }
//          dd($_POST);
        }
        $this->display();
    }
    /**
     * 信息展示
     */
    public function info(){
        $uid=session('uid');
        //查找旧数据
        $userinfo=M('userinfo')->where("uid=$uid")->find();
        $userinfo['addres']=json_decode($userinfo['addres'],true);
        $this->assign('userinfo',$userinfo);
        $this->display();
    }


    /**
     *退货记录
     */
    public function rengood(){
        //超找该用户的退货记录
        $uid=session('uid');
            $orders=M('payorder')
            ->join("sp_userorder ON sp_payorder.uoid=sp_userorder.uoid")
            ->where("sp_payorder.uid=$uid AND sp_userorder.orderstatus=4")->select();
        $goods=[];
        foreach($orders as $k=>$v){
            $goods[]=[
                'orderstatus'=>$v['orderstatus'],
                'zongjian'=>$v['count'],
                'orderpay'=>$v['orderpay'],
                'ordernumber'=>$v['ordernumber'],
                'value'=>json_decode($v['orderinfo'],true),
            ];
        }
        $this->assign('goods',$goods);
//        dd($orders);
        dd($goods);
        $this->display();
    }
    /**
     * 站内消息
     */
    public function msg(){

        $this->display();
    }
    /**
     *
     * 设为默认地址
     */
        public function moren(){
           if(IS_AJAX){
//               dd($_POST);die;
               //更改该用户所有地址状态0
               $uid=session('uid');
               M('useraddr')-> where("uid = {$uid}")->setField('poststatus',0);
                //更改该地址为默认地址
              $res= M('useraddr')-> where("udid={$_POST['udid']}")->setField('poststatus',1);

               if($res){
                   $this->ajaxReturn(['valid'=>1,'msg'=>'更改成功']);die;
               }else{
                   $this->ajaxReturn(['valid'=>0,'msg'=>'更改失败']);die;
               }
           }
    }
    /**
     * 删除地址
     */
    public  function  deladdr(){
       if(IS_AJAX){
           $res= M('useraddr')-> where("udid={$_POST['udid']}")->delete();
           if($res){
               $this->ajaxReturn(['valid'=>1,'msg'=>'删除成功']);die;
           }else{
               $this->ajaxReturn(['valid'=>0,'msg'=>'删除失败']);die;
           }
       }
    }
    /**
     * 退货
     */
    public function refund(){
        if(IS_AJAX){
//            dd($_POST);
            $ugid=I('post.ugid');
            $uid=session('uid');
            //查找退货商品信息
            $good=M('usergoods')
                ->join("sp_goods ON sp_usergoods.gid=sp_goods.gid")
                ->where("sp_usergoods.ugid=$ugid")->find();
        }else{
            $ugid=I('get.ugid');
            $uid=session('uid');
            //查找退货商品信息
            $good=M('usergoods')
                ->join("sp_goods ON sp_usergoods.gid=sp_goods.gid")
                ->where("sp_usergoods.ugid=$ugid")->find();
//        dd($good);
            $this->assign('good',$good);
        }
        if(IS_POST){
//            dd($good);
//            dd($_POST);die;
            //请用户退货信息表中插入数据,restatus表示用户正在申请退货
            $data['retime']=time();
            $data['uid']=session('uid');
            $data['gid']=$good['gid'];
            $data['pcid']=$good['pcid'];
            $data['ugid']=$ugid;
            $data['restatus']=1;
            $data['reason']=I('post.reason');
//            执行事务
            M()->startTrans();
            try{
                $row=D('userrefund')->addrefund($data);
//            更改usergoods表中的gstatus，改为4,表示正在申请退货
                $res=M('usergoods')->where("ugid=$ugid")->setField("gstatus",4);
                M()->commit();
                $this->ajaxReturn(['valid'=>1,'msg'=>'退货申请成功']);die;
            }catch (Exception $e){
                M()->rollback();
                $this->ajaxReturn(['valid'=>0,'msg'=>'退货申请失败']);die;
            }
        }
        $this->display();
    }
    /**
     * 确认收货
     */
    public function confiregood(){
     $uoid=I('get.uoid');
//        dd($uoid);die;
        //更改userorder的转态 ，将其状态更改为更改为2
        $res=M('userorder')->where("uoid=$uoid")->setField("orderstatus",2);
        //更改usergoods的gstatus的值,将其更改为3
        $good=M('userorder')->join('sp_payorder ON sp_userorder.uoid=sp_payorder.uoid')->where("sp_userorder.uoid=$uoid")->find();
        $goods=json_decode($good['orderinfo'],true);
        //获得只包含ugid 的数组
        $ugid=[];
        foreach($goods as $k=>$v){
            $ugid[]=$v['ugid'];
        }
        $map['ugid']=['in',$ugid];
        $res=M('usergoods')->where($map)->setField('gstatus',3);
        $this->redirect('home/userinfo/order');
//        dd($ugid);
    }
}